package com.leyou.item.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.leyou.common.dto.PageDTO;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entity.Spu;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.service.*;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.leyou.common.constants.MQConstants.*;

/**
 * @author ShenMin
 * @date 2021/7/18 11:11
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    SpuService spuService;
    @Autowired
    SpuDetailService detailService;
    @Autowired
    SkuService skuService;
    /*@Autowired
    SpecGroupService specGroupService;*/
    @Autowired
    SpecParamService specParamService;

    @Autowired
    AmqpTemplate amqpTemplate;

    @Override
    public PageDTO listSpuByPage(Long bid, Long cid, Long spuid, Integer page, Integer rows, Boolean isSaleable) {
        return spuService.listSpuByPage(bid, cid, spuid, page, rows, isSaleable);
    }

    @Override
    public List<SkuDTO> querySkuListByIds(List<Long> ids) {
        return skuService.querySkuListByIds(ids);
    }

    @Override
    public List<SkuDTO> querySkuListBySpuId(Long spuId) {
        return skuService.querySkuListBySpuId(spuId);
    }

    @Override
    public SpuDTO getSpuById(Long id) {
        return spuService.getSpuById(id);
    }

    @Override
    public SpuDTO getSpuOnlyById(Long id) {
        return spuService.getSpuOnlyById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSaleable(Long id, Boolean saleable) {
        spuService.updateSaleable(id, saleable);
        String routingKey = saleable ? RoutingKeyConstants.ITEM_UP_KEY:RoutingKeyConstants.ITEM_DOWN_KEY;
        amqpTemplate.convertAndSend(ExchangeConstants.ITEM_EXCHANGE_NAME,routingKey,id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSpu(SpuDTO spuDTO) {
        spuService.updateSpu(spuDTO);
    }

    @Override
    public List<SpecParamDTO> listSpecParamBySpuId(Long id, Boolean isSearch) {
        Spu spu = spuService.getById(id);
        List<SpecParamDTO> specParamDTOList = specParamService.listSpecParam(spu.getCid3(), null, isSearch);
        SpuDetail spuDetail = detailService.query().eq("spu_id", id).one();
        //Map<Long, Object> map = JsonUtils.toMap(spuDetail.getSpecification(), Long.class, Object.class);
        Map<Long, Object> map = JsonUtils.nativeRead(spuDetail.getSpecification(), new TypeReference<Map<Long, Object>>() {
        });
        List<SpecParamDTO> paramDTOList = specParamDTOList.stream().map(specParamDTO -> {
            Long paramDTOId = specParamDTO.getId();
            Object paramValue = map.get(paramDTOId);
            specParamDTO.setValue(paramValue.toString());
            return specParamDTO;
        }).collect(Collectors.toList());
        return paramDTOList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addSpu(SpuDTO spuDTO) {
        spuService.addSpu(spuDTO);
    }

    @Override
    public SpuDetailDTO querySpuDetailBySpuId(Long spuId) {
        return spuService.querySpuDetailBySpuId(spuId);
    }


}
